// 使用特殊配置适合微信小程序环境
const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  // 配置微信小程序的入口文件 - 使用多入口配置
  entry: {
    app: './app.js',
    'pages/index/index': './pages/index/index.js',
    'pages/diagnosis/diagnosis': './pages/diagnosis/diagnosis.js',
    'pages/login/login': './pages/login/login.js',
    'pages/history/history': './pages/history/history.js',
    'pages/history/detail/detail': './pages/history/detail/detail.js',
    'pages/admin/env-switcher': './pages/admin/env-switcher.js',
    'pages/admin/debug': './pages/admin/debug.js',
    'utils/api': './utils/api.js',
    'utils/debugTools': './utils/debugTools.js',
    'config/env': './config/env.js',
    'components/env-indicator/env-indicator': './components/env-indicator/env-indicator.js'
  },
  
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
    // 清空输出目录
    clean: true,
    // 微信小程序环境
    globalObject: 'wx',
    library: {
      type: 'commonjs2'
    },
    libraryTarget: 'commonjs2'
  },
  
  // 特别重要：使用正确的目标环境
  target: 'web',
  
  mode: 'development',
  
  // 不压缩代码以便调试
  optimization: {
    minimize: false,
    // 关闭代码分割
    splitChunks: false,
    // 确保每个模块独立打包
    runtimeChunk: false
  },
  
  // 配置模块解析规则
  resolve: {
    extensions: ['.js', '.json'],
    // 自定义解析路径
    alias: {
      '@': path.resolve(__dirname, './')
    }
  },
  
  // 轻量级的source map以提高性能
  devtool: 'eval-source-map',
  
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              ['@babel/preset-env', {
                targets: {
                  browsers: ['last 2 versions', 'ios >= 8', 'android >= 4.4']
                },
                // 关键：不转换模块
                modules: false,
                loose: true
              }]
            ],
            plugins: [
              // 添加一些插件以支持特定语法
              '@babel/plugin-transform-runtime'
            ]
          }
        }
      }
    ]
  },
  
  plugins: [
    // 复制非JS文件
    new CopyPlugin({
      patterns: [
        {
          from: '**/*.{wxml,wxss,json,png,jpg,gif,svg}',
          to: './',
          globOptions: {
            ignore: ['**/node_modules/**', '**/dist/**', '**/.git/**']
          },
          noErrorOnMissing: true
        }
      ]
    })
  ]
};
